package com.hibernate.daoimp.message;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.common.Constant;
import com.common.Page;
import com.hibernate.dao.message.MessageDao;
import com.hibernate.pojo.Message;
import com.struts.form.MessageForm;

public class MessageDaoImp extends HibernateDaoSupport implements MessageDao
{
	private boolean bool;
	private int record_count=0;
	/**
	 * return message list
	 */
	public Map findMessage(final String hql,int page)
	{
		Map hashmap=new HashMap();
		//record_count: page start count    current page mul page display count
		record_count=page*Constant.PAGE_VIEW_COUNT;
		//get counts
		List list=this.getHibernateTemplate().find("select count(*) from Message");
		Integer counts=(Integer)list.get(0);
		//reckon page
		Page pageobject=new Page();	
		Map map=pageobject.pageCount(counts);
		//current page
		map.put("pagecur", page);
		//page object
		hashmap.put("page_content", map);
		
		list=getHibernateTemplate().executeFind(new HibernateCallback(){
		public Object doInHibernate(Session session)throws HibernateException, SQLException
		{
			Query query;
			query = session.createQuery(hql);
			//start
			query.setFirstResult(record_count);
			//end
			query.setMaxResults(Constant.PAGE_VIEW_COUNT);
			List list = query.list();
			return list;
		}
		});
		hashmap.put("message_list", list);
		return hashmap;
	}
	
	/**
	 * save some one message
	 */
	public boolean saveMessage(Message message)
	{
		bool=false;
		try
		{
			//set save timer
			message.setCreate_time(new Date());
			this.getHibernateTemplate().save(message);
			bool=true;
		}
		catch(Exception e)
		{
			bool=false;
			e.printStackTrace();
		}
		return bool;
	}
	
	/**
	 * edit some one message
	 */
	public boolean editMessage(MessageForm messageform)
	{
		bool=false;
		return bool;
	}
	
	/**
	 * delete some one message
	 */
	public boolean delMessage(Long id)
	{
		try
		{
			Message message=(Message)this.getHibernateTemplate().load(Message.class, id);
			this.getHibernateTemplate().delete(message);
			bool=true;
		}
		catch(Exception e)
		{
			bool=false;
			e.printStackTrace();
		}
		return bool;
	}
	
	/**
	 * 
	 */
	public boolean deleteBatchMessage(String ids)
	{
		final String hql="delete from Message where id in("+ids+")";
		bool=false;
		getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session)throws HibernateException, SQLException
			{
				Query query;
				query = session.createQuery(hql);
				query.executeUpdate();
				bool=true;
				return bool;
			}
			});
		return bool;
	}
	
	/**
	 * display some one message
	 */
	public Message displayMessageInfo(Long id)
	{
		Message message=(Message)this.getHibernateTemplate().load(Message.class, id);
		return message;
		
	}
}
